package com.ivncorp.evnproject;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ivncorp.evnproject.helpers.InternetConnectionChecker;
import com.ivncorp.evnproject.helpers.LocationTracker;
import com.ivncorp.evnproject.helpers.MyMarker;
import com.ivncorp.evnproject.helpers.Utils;
import com.ivncorp.evnproject.model.ObjectTuTT;
import com.ivncorp.evnproject.upload.MyServiceTuTT;
import com.ivncorp.evnproject.upload.MyServiceTuTT.LocalBinderTuTT;
import com.ivncorp.evnproject.upload.UploadErrorCode;
import com.ivncorp.evnproject.upload.UploadFilesCompleteListener;

public class ActivityGalleryViewItemTuTT extends Activity {

	LocationTracker locationTracker;

	private ObjectTuTT object;
	private MyMarker marker;
	private ImageView ivImage;
	private TextView txtCode;
	private TextView txtName;
	private TextView txtTime;
	private Bitmap bitmap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gallery_view_item_tu_tt);
		ivImage = (ImageView) findViewById(R.id.ivViewTuTT1);
		txtCode = (TextView) findViewById(R.id.txtGalleryViewItemTuTTCode);
		txtName = (TextView) findViewById(R.id.txtGalleryViewItemTuTTName);
		txtTime = (TextView) findViewById(R.id.txtGalleryViewItemTuTTTime);

		locationTracker = new LocationTracker(this);
		Intent intent = getIntent();
		if (intent.getStringExtra("input").equals("object")) {
			object = (ObjectTuTT) intent.getSerializableExtra("object");
		} else if (intent.getStringExtra("input").equals("marker")) {

			marker = (MyMarker) intent.getSerializableExtra("mymarker");
		}
		if (object != null) {

			txtCode.setText(object.getCode());
			txtName.setText(object.getName());
			txtTime.setText(object.getTimestamp());
	

			final int width = ivImage.getLayoutParams().width;
			final int height = ivImage.getLayoutParams().height;

			
					String uri = object.getImage_1();
					if (uri != null) {
						bitmap = Utils.decodeFile(uri, width, height);
						ivImage.setImageBitmap(bitmap);
					}
			
		}
		if (marker != null) {
			txtName.setText(marker.getmLabel());
		}
		getActionBar().setDisplayShowHomeEnabled(true);
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		if (bitmap != null){
			bitmap.recycle();
			bitmap = null;
		}
		ivImage.setImageBitmap(null);;
		super.onDestroy();
	}

	public void imageclick(View view) {

		Intent intent = new Intent(this, ActivityFullScreenImageOffline.class);

		switch (view.getId()) {
		case R.id.layoutViewTuTT1:
			intent.putExtra("url", object.getImage_1());
			break;
		
		default:
			break;
		}

		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_activity_gallery_view_item, menu);
		if (object != null)
			if (object.getUploaded() == 1)
				menu.getItem(0).setEnabled(false);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.actionUpload:
			if (InternetConnectionChecker
					.isNetworkConnected(ActivityGalleryViewItemTuTT.this)) {
				new AlertDialog.Builder(ActivityGalleryViewItemTuTT.this)
						.setTitle(R.string.tai_len)
						.setMessage(R.string.ban_co_muon_tai_doi_tuong_nay_len_khong_)
						.setPositiveButton(R.string.co,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {

										List<ObjectTuTT> input = new ArrayList<ObjectTuTT>();
										input.add(object);
										Intent intent = new Intent();
										intent.setAction("uploadding");
										intent.putExtra("status", "uploadding");
										intent.putExtra("id",
												object.getId_object());
										sendBroadcast(intent);
										mService.uploadFiles(input);
										mService.setUploadFilesCompleteListener(completeListener);

									}
								})
						.setNegativeButton(R.string.khong,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										dialog.dismiss();
									}
								}).show();
			} else {
				Toast.makeText(
						getApplicationContext(),
						R.string.khong_co_ket_noi_voi_internet_vui_long_ket_noi_internet_,
						Toast.LENGTH_SHORT).show();
			}
			break;
		case android.R.id.home:
			finish();
			break;

		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	/*----------------upload-------------------*/
	boolean mBound;
	MyServiceTuTT mService;
	private ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mBound = false;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// Bound to MyService.
			LocalBinderTuTT binder = (LocalBinderTuTT) service;
			mService = binder.getService();
			mBound = true;
		}
	};

	@Override
	protected void onStart() {
		super.onStart();
		// bind to service
		Intent i = new Intent(this, MyServiceTuTT.class);
		bindService(i, mConnection, Context.BIND_AUTO_CREATE);
	}

	@Override
	protected void onStop() {
		super.onStop();
		// Unbind from the service
		if (mBound) {
			unbindService(mConnection);
			mBound = false;
		}
	}

	UploadFilesCompleteListener completeListener = new UploadFilesCompleteListener() {

		@Override
		public void onCompleted(UploadErrorCode errorCode) {

			Intent intent = new Intent();
			intent.setAction("uploadding");
			intent.putExtra("status", "uploadComplete");
			intent.putExtra("id", object.getId_object());
			sendBroadcast(intent);

			Toast.makeText(getApplicationContext(), R.string.tai_len_thanh_cong_,
					Toast.LENGTH_SHORT).show();

		}
	};

}
